<?php

namespace App\Http\Controllers\Api;

use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use App\Repos\UserRepo; 
use App\Services\RedisService;

class IndexController extends Controller
{
    public function login(Request $request)
    {
    	$rs['status'] = true;
    	$where['username'] = $request->username;
    	$password = trim($request->password);
    	$user = UserRepo::find($where);
    	if($user) {
    		$pass = md5($password.$user->code);
    		if($user->password==$pass) {
    			$data['token'] = $user->token;
    			return response()->json($rs);
    		}
    		$rs['status'] = false;
    		$rs['msg']    = '密码错误';
    		return response()->json($rs);
    	}

    }

    public function bindMobile(Request $request)
    {
    	
    }

    public function bindCode(Request $request)
    {
    	$rs['status'] = true;
    	$invite_code = $request->invite_code;
    	// 查询邀请码是否存在
    	$where['code'] = $invite_code;
    	$user = UserRepo::find($where);
    	if(!$user) {
    		$rs['status'] = false;
    		$rs['msg'] = '邀请码错误';
    		return response()->json($rs);
    	}
    	return response()->json($rs);

    }

    public function sendMsg(Request $request)
    {
    	$rs['status'] = true;
    	$code = mt_rand(1000,9999);
    	RedisService::set($key,$code);
    	$rs['data'] = $code;
    	return response()->json($rs);
    }
}
